package cn.doitedu.datayi.data_service.dao;

import cn.doitedu.datayi.data_service.po.FunnelStatistic;
import org.springframework.stereotype.Repository;

import java.sql.*;
import java.util.ArrayList;
import java.util.Properties;

@Repository
public class PrestoDaoImpl implements PrestoDao {
    @Override
    public ArrayList<FunnelStatistic> queryFunnel(String sql) throws Exception {

        String url = "jdbc:presto://hdp01:8080/hive/dwd";
        Properties properties = new Properties();
        properties.setProperty("user", "root");
        properties.setProperty("SSL", "false");

        Class.forName("io.prestosql.jdbc.PrestoDriver");
        Connection conn = DriverManager.getConnection(url, properties);

        Statement stmt = conn.createStatement();
        ResultSet resultSet = stmt.executeQuery(sql);


        ArrayList<FunnelStatistic> pos = new ArrayList<>();
        while(resultSet.next()){
            long step1 = resultSet.getLong(1);
            long step2 = resultSet.getLong(2);
            long step3 = resultSet.getLong(3);
            FunnelStatistic po1 = new FunnelStatistic(1, step1);
            pos.add(po1);
            FunnelStatistic po2 = new FunnelStatistic(2, step2);
            pos.add(po2);
            FunnelStatistic po3 = new FunnelStatistic(3, step3);
            pos.add(po3);

        }


        return pos;
    }
}
